package com.myzf.ncov.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.xuxiaoxiao.chatapi.wechat.WeChatClient;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXContact;
import me.xuxiaoxiao.chatapi.wechat.entity.message.WXLocation;
import me.xuxiaoxiao.chatapi.wechat.entity.message.WXMessage;
import me.xuxiaoxiao.chatapi.wechat.entity.message.WXText;
import me.xuxiaoxiao.chatapi.wechat.entity.message.WXVerify;
import org.springframework.lang.Nullable;


import javax.annotation.Nonnull;


/**
 * @package com.myzf.ncov.utils
 * @Date Created in 2020/2/6 20:09
 * @Author myzf
 */
public class WeChatUtil {

    public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create();

    private volatile static WeChatClient weChatClient = null;

    private WeChatUtil(){}

    public static WeChatClient getWeChatClient(){
        try {
            if(weChatClient!=null){}
            else {
                synchronized (WeChatUtil.class){
                    if(weChatClient == null){
                        weChatClient = new WeChatClient();
                        //为模拟微信客户端设置监听器
                        weChatClient.setListener(LISTENER);
                        //启动模拟微信客户端
                        weChatClient.startup();
                    }
                }
            }

        }catch (Exception e){

        }
        return weChatClient;
    }

    /**
     * 新建一个微信监听器
     */
    public static final WeChatClient.WeChatListener LISTENER = new WeChatClient.WeChatListener() {
        @Override
        public void onQRCode(@Nonnull me.xuxiaoxiao.chatapi.wechat.WeChatClient client, @Nonnull String qrCode) {
            System.out.println("onQRCode：" + qrCode);
        }

        @Override
        public void onLogin(@Nonnull me.xuxiaoxiao.chatapi.wechat.WeChatClient client) {
            System.out.println(String.format("onLogin：您有%d名好友、活跃微信群%d个", client.userFriends().size(), client.userGroups().size()));
        }

        @Override
        public void onMessage(@Nonnull me.xuxiaoxiao.chatapi.wechat.WeChatClient client, @Nonnull WXMessage message) {
          //  System.out.println("获取到消息：" + GSON.toJson(message));

            if (message instanceof WXVerify) {
                //是好友请求消息，自动同意好友申请
                client.passVerify((WXVerify) message);
            } else if (message instanceof WXLocation && message.fromUser != null && !message.fromUser.id.equals(client.userMe().id)) {
                // 如果对方告诉我他的位置，发送消息的不是自己，则我也告诉他我的位置
                if (message.fromGroup != null) {
                    // 群消息
                    // client.sendLocation(message.fromGroup, "120.14556", "30.23856", "我在这里", "西湖");
                } else {
                    // 用户消息
                    client.sendLocation(message.fromUser, "120.14556", "30.23856", "我在这里", "西湖");
                }
            } else if (message instanceof WXText && message.fromUser != null && !message.fromUser.id.equals(client.userMe().id)) {
                //是文字消息，并且发送消息的人不是自己，发送相同内容的消息
                if (message.fromGroup != null) {
                    // 群消息
                    // client.sendText(message.fromGroup, message.content);
                } else {
                    // 用户消息
                    //client.sendText(message.fromUser, message.content);
                }
            }
        }

        @Override
        public void onContact(@Nonnull me.xuxiaoxiao.chatapi.wechat.WeChatClient client, @Nullable WXContact oldContact, @Nullable WXContact newContact) {
         //   System.out.println(String.format("检测到联系人变更:旧联系人名称：%s:新联系人名称：%s", (oldContact == null ? "null" : oldContact.name), (newContact == null ? "null" : newContact.name)));
        }
    };
}
